//获取应用实例
const app = getApp()
let api = require("../../utils/api.js")
Page({
  data: {
    showPayPwdInput: false,  //是否展示密码输入层
    pwdVal: '',  //输入的密码
    payFocus: true, //文本框焦点
    orderId :'',
  },
  onLoad(options) {
    // 订单id
    let Id = options.Id;
    let orderId = options.orderId;
    // console.log(options,'MMMMMMM')
    // console.log(Id,'订单id')
    this.showInputLayer();
    this.setData({
      orderId:orderId
    })
    console.log(this.data.orderId,'onluch')
  },
  /**
   * 显示支付密码输入层
   */
  showInputLayer(){
    
    this.setData({ showPayPwdInput: true, payFocus: true });
  },
  /**
   * 隐藏支付密码输入层
   */
  sleep(numberMillis) { 
    var now = new Date(); 
    var exitTime = now.getTime() + numberMillis; 
    while (true) { 
    now = new Date(); 
    if (now.getTime() > exitTime) 
    return; 
    } 
    },
    // 修改订单
  editOrder:function (params) {
    var token = wx.getStorageSync('token')
    let order_id = this.data.orderId
    console.log(order_id,'>>>>>>')
    wx.request({
      url:api.editOrderApi + order_id+'/',
      data:{order_id:order_id},
      dataType: "json",
      header: {'Authorization':'JWT'+' '+token},
      method: "patch",
      success: (result) => {
        console.log(result.data,'>>>>')
        if (result.data.code!=200){
          wx.showToast({
            title: '请登陆',
            icon:"error"
          })
          return
        }else{
          // 下单操作
          // this.setData({
          //   goods:result.data
          // })
          this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: '' }, function(){
            /**弹框**/
            wx.showToast({
              title: "支付成功",
            })
            // 发送请求，修改订单状态
      
            // 页面跳转
            wx.switchTab({
              url: '/pages/orders/orders'
            })
          });

        }
        
      },
    })

  },
  hidePayLayer(){
    /**获取输入的密码**/
    var val = this.data.pwdVal;
    // console.log(val,'>>>>>123>')
  /**在这调用支付接口**/
  if(val==='123456'){
    console.log('进行支付')
    this.editOrder()
    
 
  }else{
    wx.showToast({
      title: '支付失败',
      'icon':'error'
    })
    this.sleep(1500)
    wx.switchTab({
      url: '/pages/orders/orders'
    })
  }

  },
  /**
   * 获取焦点
   */
  getFocus(){
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd(e){
      this.setData({ pwdVal: e.detail.value });
 
      if (e.detail.value.length >= 6){
        this.hidePayLayer();
      }
  }
})